﻿using ICSharpCode.AvalonEdit.Document;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LunarSF.SHomeWorkshop.LunarMarkdownEditor
{
    class AutoReplaceManager
    {

        /// <summary>
        /// 可以考虑实现这样的功能：
        /// 输入如下文本：
        ///     今天的日期是date
        /// 然后按 Tab 键，自动变成：
        ///     今天的日期是2018年3月15日 
        /// </summary>
        /// <returns>执行成功返回 true 。</returns>
        public static bool Replace(TextDocument document, int selectionStart, int selectionLength)
        {
            if (selectionLength > 0) return false;

            if (autoReplaceTextList == null)
            {
                autoReplaceTextList = new List<AutoReplaceItem>();
                autoReplaceTextList.Add(new AutoReplaceItem() { TextTag = "date", Method = Replace_date, });
                autoReplaceTextList.Add(new AutoReplaceItem() { TextTag = "Date", Method = Replace_Date, });
                autoReplaceTextList.Add(new AutoReplaceItem() { TextTag = "DATE", Method = Replace_DATE, });
                autoReplaceTextList.Add(new AutoReplaceItem() { TextTag = "time", Method = Replace_time, });
                autoReplaceTextList.Add(new AutoReplaceItem() { TextTag = "Time", Method = Replace_Time, });
                autoReplaceTextList.Add(new AutoReplaceItem() { TextTag = "TIME", Method = Replace_TIME, });

                //TODO： 可在此处添加其它要替换的文本。
            }

            try
            {
                var line = document.GetLineByOffset(selectionStart);
                var previewText = document.GetText(line.Offset, selectionStart - line.Offset);

                //遍历，执行其中对应的项的委托方法

                foreach (var item in autoReplaceTextList)
                {
                    if (previewText.EndsWith(item.TextTag))
                    {
                        if (item.Method(document, item.TextTag, line, selectionStart, previewText)) return true;
                        //else continue;  //继续找下一个。
                    }
                }

                return false;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 按Tab时将当前插入点前的 date 替换为当前日期的小写形式。
        /// </summary>
        /// <returns></returns>
        private static bool Replace_date(TextDocument document, string tagText, DocumentLine line, int selectionStart, string previewText)
        {
            try
            {
                var tagLength = tagText.Length;
                document.Replace(selectionStart - tagLength, tagLength, DateTime.Now.ToShortDateString());
                return true;
            }
            catch { return false; }
        }

        /// <summary>
        /// 按Tab时将当前插入点前的 date 替换为当前日期的“xxxx年xx月xx日”形式。
        /// </summary>
        /// <returns></returns>
        private static bool Replace_Date(TextDocument document, string tagText, DocumentLine line, int selectionStart, string previewText)
        {
            try
            {
                var tagLength = tagText.Length;
                var now = DateTime.Now;
                var dateText = now.Year + "年" + now.Month + "月" + now.Day + "日";
                document.Replace(selectionStart - tagLength, tagLength, dateText);
                return true;
            }
            catch { return false; }
        }

        /// <summary>
        /// 按Tab时将当前插入点前的 date 替换为当前日期的纯中文形式。
        /// </summary>
        /// <returns></returns>
        private static bool Replace_DATE(TextDocument document, string tagText, DocumentLine line, int selectionStart, string previewText)
        {
            try
            {
                var tagLength = tagText.Length;
                var now = DateTime.Now;
                var dateText = DateTimeConverter.YearToCn(now.Year) + "年" + DateTimeConverter.MonthToCn(now.Month) + "月" + DateTimeConverter.DayToCn(now.Day) + "日";
                document.Replace(selectionStart - tagLength, tagLength, dateText);
                return true;
            }
            catch { return false; }
        }

        /// <summary>
        /// 按Tab时将当前插入点前的 time 替换为当前时间的小写形式。
        /// </summary>
        /// <returns></returns>
        private static bool Replace_time(TextDocument document, string tagText, DocumentLine line, int selectionStart, string previewText)
        {
            try
            {
                var tagLength = tagText.Length;
                document.Replace(selectionStart - tagLength, tagLength, DateTime.Now.ToShortTimeString());
                return true;
            }
            catch { return false; }
        }

        /// <summary>
        /// 按Tab时将当前插入点前的 time 替换为当前日期的“xxxx时xx分xx秒”形式。
        /// </summary>
        /// <returns></returns>
        private static bool Replace_Time(TextDocument document, string tagText, DocumentLine line, int selectionStart, string previewText)
        {
            try
            {
                var tagLength = tagText.Length;
                var now = DateTime.Now;
                var timeText = now.Hour + "时" + now.Minute + "分" + now.Second + "秒";
                document.Replace(selectionStart - tagLength, tagLength, timeText);
                return true;
            }
            catch { return false; }
        }

        /// <summary>
        /// 按Tab时将当前插入点前的 date 替换为当前日期的纯中文形式。
        /// </summary>
        /// <returns></returns>
        private static bool Replace_TIME(TextDocument document, string tagText, DocumentLine line, int selectionStart, string previewText)
        {
            try
            {
                var tagLength = tagText.Length;
                var now = DateTime.Now;
                var timeText = DateTimeConverter.HourToCn(now.Hour) + "时" + DateTimeConverter.MinuteOrSecondToCn(now.Minute) + "分" + DateTimeConverter.MinuteOrSecondToCn(now.Second) + "秒";
                document.Replace(selectionStart - tagLength, tagLength, timeText);
                return true;
            }
            catch { return false; }
        }

        private static List<AutoReplaceItem> autoReplaceTextList = null;

    }
}
